﻿Public Class ChoixDossierMedical_Screen


    Private _ctrl As ChoixDossierMedical_CTRL
    Private _init As Boolean = False

#Region "Cycle de vie de la fenetre CHoixDossierMedical"

    Public Sub New(ByVal controleur As ChoixDossierMedical_CTRL)

        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        _ctrl = controleur

    End Sub

    Private Sub ChoixDossierMedical_Screen_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed

        _ctrl.Dispose()

    End Sub

    Private Sub ChoixDossierMedical_Screen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        RadTous.Checked = True
        BarBoutonCHOIXdossierMEDICAL.BtnDossierMedicaux.Visible = True

    End Sub

#End Region

    Private Sub refreshTreeView()

        TVWdossierMedical.Nodes.Clear()
        Dim indexAnimal As Integer = 0
        For i = 0 To _ctrl.listeClient.Count - 1
            indexAnimal = 0
            TVWdossierMedical.Nodes.Add(_ctrl.listeClient(i).ToString)
            For Each animal In _ctrl.listeClient(i).listeAnimauxClient
                Dim noeud As New TreeNode(animal.ToString)
                TVWdossierMedical.Nodes(i).Nodes.Add(noeud)
                TVWdossierMedical.Nodes(i).Nodes(indexAnimal).Tag = animal
                indexAnimal += 1
            Next
            TVWdossierMedical.Nodes(i).Expand()
        Next

    End Sub

    Private Sub RadTous_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadTous.CheckedChanged

        If RadTous.Checked = True Then
            CmbNom.DataSource = Nothing
            CmbNom.Enabled = False
            CmbPrenom.Enabled = False
            CmbNom.SelectedIndex = -1
            CmbPrenom.SelectedIndex = -1
            _ctrl.getAllClients()
            refreshTreeView()
        End If

    End Sub

    Private Sub RadUnClient_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadUnClient.CheckedChanged

        If RadUnClient.Checked = True Then

            CmbNom.DataSource = _ctrl.getNomCLient
            CmbNom.Enabled = True
            CmbPrenom.Enabled = True
            CmbNom.SelectedIndex = -1
            CmbPrenom.SelectedIndex = -1

        End If

    End Sub

    Private Sub TVWdossierMedical_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TVWdossierMedical.AfterSelect

        If TypeOf (e.Node.Tag) Is Animaux Then
            _ctrl.animalCourant = e.Node.Tag
            BarBoutonCHOIXdossierMEDICAL.BtnDossierMedicaux.Enabled = True
        Else
            _ctrl.animalCourant = Nothing
            BarBoutonCHOIXdossierMEDICAL.BtnDossierMedicaux.Enabled = False
        End If

    End Sub

    Private Sub BarBoutonCHOIXdossierMEDICAL_Action(ByVal sender As Object, ByVal e As ActionEventArgs) Handles BarBoutonCHOIXdossierMEDICAL.Action

        If e.Action = actionEnum.LienDossierMedical Then

            Dim frm As New DossierMedical_Screen(Main_CTRL.getInstance.getDossierMedical_CTRL(_ctrl.animalCourant))

            frm.StartPosition = FormStartPosition.CenterScreen

            frm.ShowDialog()

        End If


    End Sub

    Private Sub CmbNom_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbNom.TextChanged

        _init = True

        _ctrl.rechercheClientByNom(CmbNom.Text)
        CmbPrenom.DataSource = Nothing
        CmbPrenom.DataSource = _ctrl.getPrenomClient
        CmbPrenom.SelectedIndex = -1
        refreshTreeView()

        _init = False

    End Sub

    Private Sub CmbPrenom_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbPrenom.TextChanged

        If Not String.IsNullOrEmpty(CmbPrenom.Text) And Not _init Then

            _ctrl.rechercheClientByNomAndPrenom(CmbNom.Text, CmbPrenom.SelectedItem)
            refreshTreeView()

        End If

    End Sub
End Class